/**
 * Copyright (C)  Gadglet .
 *
 * This file is part of Gadglet
 *
 * Gadglet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gadglet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Gadglet. If not, see <http://www.gnu.org/licenses/>.
 */

package com.gadglet.client.gwt.ui;

import java.util.ArrayList;

import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;

public class VerticalFormPanel extends Composite{
	private final VerticalPanel form = new VerticalPanel();
	private String titlesWidth;
	private String controlWidth;
	
	public VerticalFormPanel(){
		initWidget(form);
		}
	
	public void setTitlesWidth(String width){
		titlesWidth = width;
	}
	
	public void setControlWidth(String width){
		controlWidth = width;
	}
	
	public VerticalPanel getFormPanel(){
		return form;
	}
	
	public FormItem getFormItem(String name){
		
		FormItem item = null;
		String 	fieldName  =null;
		for(int index = 0; index< form.getWidgetCount();index++){
			fieldName = ((FormItem)(form.getWidget(index))).getFieldName();
		
			if(name.equals(fieldName))
				item = (FormItem)(form.getWidget(index));
		
		}
		
		return item;
	}
	
	
	public void add(FormItem item){
		if(titlesWidth!=null)
			item.setTitleWidth(titlesWidth);
		
		if(controlWidth!=null)
			item.setControlWidth(controlWidth);
		
		
		
		form.add(item);
	}
	
	
	
	public void setFormValues(JSONObject currentValues){
		String fieldName ;
		String currentValue ;
		try {
			for(int index = 0; index< form.getWidgetCount();index++)
			{
				fieldName = null;
				currentValue =null;
				
				fieldName = ((FormItem)(form.getWidget(index))).getFieldName();
				if(currentValues.get(fieldName)!=null){
					currentValue = currentValues.get(fieldName).isString().stringValue();
					((FormItem)(form.getWidget(index))).setFieldControlValue(currentValue);
				}
				
			}
		} catch (Exception e) {
		
		}
		
	}
	
	
	public ArrayList <FromItemsArray> getFromValues(){
		ArrayList <FromItemsArray> items = new ArrayList <FromItemsArray>();
		
		try {
			for(int index = 0; index< form.getWidgetCount();index++)
				items.add(new FromItemsArray(((FormItem)(form.getWidget(index))).getFieldName(),((FormItem)form.getWidget(index)).getFieldValue() ) );
		} catch (Exception e) {
		
		}
	return items;
	}
	
	public class FromItemsArray{
		String name =null;
		String value=null;
		
		FromItemsArray(String name, String value){
			this.name = name;
			this.value = value;
		}

		public String getName() {
			return name;
		}

		void setName(String name) {
			this.name = name;
		}

		public String getValue() {
			return value;
		}

		void setValue(String value) {
			this.value = value;
		}
		
		
	}
	
}

